package com.yDev.stock_runner;

import java.util.ArrayList;
import java.util.Random;

import com.yDev.stock_runner.MainThread;

import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class SR_Level_Encasing extends SurfaceView implements SurfaceHolder.Callback {

	private MainThread thread;
	
	private SR_Level level;
	
	RigidBody test=new RigidBody();
	RigidBody test2=new RigidBody();
	
	RigidBody floorBody=new RigidBody();
	
	ydPoint clickedAt=new ydPoint();
	ydPoint POC=new ydPoint();
	
	ArrayList<RigidBody> rBodies =new ArrayList<RigidBody>();
	
	ParticleEmitter pm =new ParticleEmitter(100);
	
	int i=0;
	
	ydForce gravity;
	
	public SR_Level_Encasing(Context context) {
		super(context);
		
		getHolder().addCallback(this);
		setFocusable(true);

		thread = new MainThread(getHolder(), this);
		
		level =new SR_Level(getContext(),"BVSN");
		
		Particle.img=BitmapFactory.decodeResource(getResources(), R.drawable.particle_texture3);
		Particle.drawable=context.getResources().getDrawable(R.drawable.particle_texture3);
		test.position=new ydPoint(10, 600);
		test.size.x=200;
		test.size.y=200;
		
		test.theta=Globals.degreesToRads(75);
		test.MASS=20;
		
		test.calcCornerPoints();
		test.calculateCenterMass();
		
		test2.position=new ydPoint(175, 10);
		test2.size.x=100;
		test2.size.y=100;
		
		test2.theta=Globals.degreesToRads(0);
		test2.MASS=20;
		
		test2.calcCornerPoints();
		test2.calculateCenterMass();
		
		floorBody.position=new ydPoint(0, 750);
		floorBody.size.x=500;
		floorBody.size.y=20;
		
		floorBody.theta=Globals.degreesToRads(0);
		floorBody.MASS=20;
		
		floorBody.calcCornerPoints();
		floorBody.calculateCenterMass();
		
		gravity=new ydForce(new ydPoint(), new ydPoint(0,-10), true);
		
		test.addForce(gravity);
		//test2.velocity=new ydPoint(-1,-1);
		/*Random rand =new Random(500);
		
		for (int i=0; i<8; i++){
			RigidBody t=new RigidBody();
			if(i==0){
			t.addForce(gravity);
			t.position=new ydPoint(0, 0);
			t.size.x=300;
			t.size.y=20;
				//t.velocity=new ydPoint(0, 1);
			}
			else{
			t.position=new ydPoint(rand.nextInt(400), rand.nextInt(700));
			t.size.x=rand.nextInt(50)+5;
			t.size.y=rand.nextInt(50)+5;
			}
			t.theta=0;//Globals.degreesToRads(rand.nextInt(360));
			t.MASS=30;
			
			t.calcCornerPoints();
			t.calculateCenterMass();
			
			rBodies.add(t);
		}*/
		
		// TODO Auto-generated constructor stub
	}
	
	ydLine floor=new ydLine(new ydPoint(1, 0), new ydPoint(0, 750));

	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
	}

	public void surfaceCreated(SurfaceHolder holder) {
		thread.setRunning(true);
		thread.start();
		level.startDataCollection();
		
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		/*boolean retry = true;
		while (retry) {
			try {
				thread.join();
				retry = false;
			} catch (InterruptedException e) {
				// try again shutting down the thread
			}
		}*/
	}
	
	Paint paint=new Paint();
	
	@Override
	protected void onDraw(Canvas canvas) {

		super.onDraw(canvas);
		
		canvas.drawColor(Color.BLACK);
		i++;
		canvas.drawText(Integer.toString(i), 20, 20, paint);
		
		
/*		level.drawTerrian(canvas);
		
		for(RigidBody r: rBodies){
			r.drawBody(canvas, paint);
		}*/
		pm.emitParticle();
		pm.update();
		pm.draw(canvas);
		
/*		test.drawBody(canvas, paint);
		test2.drawBody(canvas, paint);
		
		floorBody.drawBody(canvas, paint);
		
		RigidBody.POCTest(floorBody, test, canvas, paint);
		RigidBody.POCTest(floorBody, test2, canvas, paint);*/
		//RigidBody.collisionOccured2(test, test2, canvas, paint);
		
/*		for(RigidBody r: rBodies){
			for(RigidBody r2: rBodies){
				RigidBody.collisionOccured(r, r2, canvas, paint);
				r.calculateNextPosition(new ydPoint());
				r2.calculateNextPosition(new ydPoint());
			}
		}*/
		
/*		for(RigidBody r: rBodies){
			r.drawBody(canvas, paint);
		}
		*/
		
		
		canvas.drawText(clickedAt.toString(), 20, 50, paint);
		/*if(RigidBody.POCTest(test, test2, canvas, paint)){
			test.addForce(gravity);
			test2.addForce(gravity);
		}*/
		//POC.drawPoint(canvas, paint);
		
		floor.drawLine(canvas, paint);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		clickedAt.x=event.getX();
		clickedAt.y=event.getY();
		test2.translateBodyTo(new ydPoint(event.getX(), event.getY()));
		invalidate();
		return true;

	}

	public void update() {
		// TODO Auto-generated method stub
		//level.startDataCollection();
		//level.update();

		
		
		
		if(RigidBody.collisionOccured(test, test2)){

			 Log.d("collsionCheck", "Colliding");
			paint.setColor(Color.RED);
			
			
		}
		else{
			paint.setColor(Color.GREEN);
		}

		test2.calculateNextPosition(new ydPoint());
		test.calculateNextPosition(new ydPoint());
		
		/*(rBodies.get(0)).addForce(gravity);
				for(RigidBody r: rBodies){
		for(RigidBody r2: rBodies){
			RigidBody.collisionOccured(r, r2);
			r.calculateNextPosition(new ydPoint());
			r2.calculateNextPosition(new ydPoint());
		}*/
	
		
	//test.calculateAngularVelocityTotal();
	
	}
	


}
